<% 
if(!("admin".equals(session.getValue("status")))) {
out.println("<script language='javascript'>parent.location.replace('../index.jsp');</script>");
}
else {
%>
<%@ include file="../includes/config.inc.jsp" %>
<%@ include file="../includes/header.inc.jsp" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>


<%!
private String parseFieldType(String ftype)
{
String parsedtype=ftype;
if (ftype.indexOf("unsigned")>-1)
{int unsindex=ftype.indexOf("unsigned"); parsedtype=ftype.substring(0,unsindex); parsedtype="unsigned "+parsedtype;}
else if (ftype.indexOf("signed")>-1)
{int sindex=ftype.indexOf("signed"); parsedtype=ftype.substring(0,sindex); parsedtype="signed "+parsedtype;}
return parsedtype.toUpperCase();
}
%>
<%
try{
Class.forName(JdbcDriver).newInstance();
Connection con =DriverManager.getConnection(Url ,DbUser,DbPass);

if(request.getParameter("tblcount")!=null&&request.getParameter("db")!=null)
{	
	String database = request.getParameter("db");	
	int count = new Integer(request.getParameter("tblcount")).intValue();
	String modus = request.getParameter("exportmodus");	
	String date = new SimpleDateFormat("dd-MM-yyyy  hh:mm").format(new Date());
	String structout="";	String dataout="";
	String header="#*********************************************************************************************************************<br>";
	header=header+"#<b>MySQL Dump generated by JspSqlMyAdmin</b><br>#<b>Creation Date: "+date+"</b><br>#<b>MySQL Host :"+DbHost+"</b>";
	header=header+"<br>#*********************************************************************************************************************<br>";
	for (int i=1;i<=count;i++)
	{
	if(request.getParameter("expcbox"+i)!=null)
	{
	String tblname=request.getParameter("expcboxname"+i);
	if (modus.equals("datandstruct"))
	{
	dataout=dataout+"<b>#Content of the table "+tblname+"</b><br>";
	Statement datastat = con.createStatement();
	ResultSet datares = datastat.executeQuery("SELECT * FROM "+database+"."+tblname);
	int colcount = datares.getMetaData().getColumnCount();
	while(datares.next()){
	dataout=dataout+"INSERT INTO "+database+"."+tblname+" VALUES (";
	String mytmp="";
	for(int j=1;j<=colcount;j++) {mytmp=mytmp+"'"+datares.getString(j)+"',";}
	mytmp=mytmp.substring(0,mytmp.lastIndexOf(','));
	dataout=dataout+mytmp+");<br>";
	}
	}
	structout=structout+"<b>#Structure of the table "+tblname+"</b><br>"+"CREATE TABLE "+database+"."+tblname+"(";
	String tmp="";
	Statement structstat = con.createStatement();
	ResultSet structres = structstat.executeQuery("SHOW COLUMNS FROM "+database+"."+tblname);
	while(structres.next()){	
	tmp=tmp+structres.getString("Field")+" "+parseFieldType(structres.getString("Type"))+" ";
	String nullparam=""; if (structres.getString("Null").equals("")) {nullparam="NOT NULL";}
	tmp=tmp+nullparam+" ";
	String defaultparam=""; if (structres.getString("Default")!=null) {defaultparam="DEFAULT '"+structres.getString("Default")+"'";}
	tmp=tmp+defaultparam+" ";
	String extraparam=""; if (!structres.getString("Extra").equals("")) {extraparam=structres.getString("Extra").toUpperCase();}
	tmp=tmp+extraparam+" ";
	String keyparam=""; 
	if (structres.getString("Key").equals("PRI")) {keyparam="PRIMARY KEY ("+structres.getString("Field")+")";}
	else if (structres.getString("Key").equals("UNI")) {keyparam="UNIQUE ("+structres.getString("Field")+")";}
	else if (structres.getString("Key").equals("IND")) {keyparam="INDEX ("+structres.getString("Field")+")";}
	tmp=tmp+keyparam+",";
	}
	tmp=tmp.substring(0,tmp.lastIndexOf(','));
	structout=structout+tmp+");<br>";
	}		
	}
	
	out.println("<table border='0' align='center'><tr><td bgcolor='#EEEEEE'>"+header+structout+dataout+"</td></tr></table>");
}
con.close();
}
catch (Exception ex){out.println("The following error has occured : <font color='red'>"+ex.getMessage()+"</font><br><br><a href='javascript:window.history.back()'>Back</a>");}

%>
<%@ include file="../includes/footer.inc.jsp" %>
<%
}
%>




